home *** CD-ROM | disk | FTP | other *** search
/ Windows 95 API Bible / Windows 95 API Bible 3 Disc Set.iso / Win32 API Bible Book 1 of 3 / CHAPTE14 / GETKRNPR.C < prev    next >
C/C++ Source or Header  |  1996-01-31  |  8KB  |  222 lines

  1.  
  2. #include <windows.h>  
  3. #include "GetKrnPr.h" 
  4.  
  5.  
  6. #if defined (WIN32)
  7.     #define IS_WIN32 TRUE
  8. #else
  9.     #define IS_WIN32 FALSE
  10. #endif
  11.  
  12. #define IS_NT      IS_WIN32 && (BOOL)(GetVersion() < 0x80000000)
  13. #define IS_WIN32S  IS_WIN32 && (BOOL)(!(IS_NT) && (LOBYTE(LOWORD(GetVersion()))<4))
  14. #define IS_WIN95   (BOOL)(!(IS_NT) && !(IS_WIN32S)) && IS_WIN32
  15.  
  16. HINSTANCE hInst;   // current instance
  17.  
  18. LPCTSTR lpszAppName  = "MyApp";
  19. LPCTSTR lpszTitle    = "My Application"; 
  20.  
  21. BOOL RegisterWin95( CONST WNDCLASS* lpwc );
  22.  
  23. int APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevInstance,
  24.                       LPTSTR lpCmdLine, int nCmdShow)
  25. {
  26.    MSG      msg;
  27.    HWND     hWnd; 
  28.    WNDCLASS wc;
  29.  
  30.    // Register the main application window class.
  31.    //............................................
  32.    wc.style         = CS_HREDRAW | CS_VREDRAW;
  33.    wc.lpfnWndProc   = (WNDPROC)WndProc;       
  34.    wc.cbClsExtra    = 0;                      
  35.    wc.cbWndExtra    = 0;                      
  36.    wc.hInstance     = hInstance;              
  37.    wc.hIcon         = LoadIcon( hInstance, lpszAppName ); 
  38.    wc.hCursor       = LoadCursor(NULL, IDC_ARROW);
  39.    wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
  40.    wc.lpszMenuName  = lpszAppName;              
  41.    wc.lpszClassName = lpszAppName;              
  42.  
  43.    if ( IS_WIN95 )
  44.    {
  45.       if ( !RegisterWin95( &wc ) )
  46.          return( FALSE );
  47.    }
  48.    else if ( !RegisterClass( &wc ) )
  49.       return( FALSE );
  50.  
  51.    hInst = hInstance; 
  52.  
  53.    // Create the main application window.
  54.    //....................................
  55.    hWnd = CreateWindow( lpszAppName, 
  56.                         lpszTitle,    
  57.                         WS_OVERLAPPEDWINDOW, 
  58.                         CW_USEDEFAULT, 0, 
  59.                         CW_USEDEFAULT, 0,  
  60.                         NULL,              
  61.                         NULL,              
  62.                         hInstance,         
  63.                         NULL               
  64.                       );
  65.  
  66.    if ( !hWnd ) 
  67.       return( FALSE );
  68.  
  69.    ShowWindow( hWnd, nCmdShow ); 
  70.    UpdateWindow( hWnd );         
  71.  
  72.    while( GetMessage( &msg, NULL, 0, 0) )   
  73.    {
  74.       TranslateMessage( &msg ); 
  75.       DispatchMessage( &msg );  
  76.    }
  77.  
  78.    return( msg.wParam ); 
  79. }
  80.  
  81.  
  82. BOOL RegisterWin95( CONST WNDCLASS* lpwc )
  83. {
  84.    WNDCLASSEX wcex;
  85.  
  86.    wcex.style         = lpwc->style;
  87.    wcex.lpfnWndProc   = lpwc->lpfnWndProc;
  88.    wcex.cbClsExtra    = lpwc->cbClsExtra;
  89.    wcex.cbWndExtra    = lpwc->cbWndExtra;
  90.    wcex.hInstance     = lpwc->hInstance;
  91.    wcex.hIcon         = lpwc->hIcon;
  92.    wcex.hCursor       = lpwc->hCursor;
  93.    wcex.hbrBackground = lpwc->hbrBackground;
  94.    wcex.lpszMenuName  = lpwc->lpszMenuName;
  95.    wcex.lpszClassName = lpwc->lpszClassName;
  96.  
  97.    // Added elements for Windows 95.
  98.    //...............................
  99.    wcex.cbSize = sizeof(WNDCLASSEX);
  100.    wcex.hIconSm = LoadImage(wcex.hInstance, lpwc->lpszClassName, 
  101.                             IMAGE_ICON, 16, 16,
  102.                             LR_DEFAULTCOLOR );
  103.             
  104.    return RegisterClassEx( &wcex );
  105. }
  106.  
  107. LRESULT CALLBACK WndProc( HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
  108. {
  109.    switch( uMsg )
  110.    {
  111.       case WM_COMMAND :
  112.               switch( LOWORD( wParam ) )
  113.               {
  114.                  case IDM_TEST :
  115.                         {
  116.                            HDC            hDC;
  117.                            HFONT          hFont, hOldFont;
  118.                            KERNINGPAIR*   lpKp;
  119.                            DWORD          dwRet;
  120.                            int            i;
  121.                            int            nTabs[2];
  122.                            char           szMsg[50];
  123.  
  124.                            // Create a Times New Roman font.
  125.                            //...............................
  126.                            hFont = CreateFont( 16, 0, 0, 0, FW_NORMAL,
  127.                                                FALSE, FALSE, FALSE,
  128.                                                ANSI_CHARSET, 
  129.                                                OUT_DEFAULT_PRECIS,
  130.                                                CLIP_DEFAULT_PRECIS,
  131.                                                PROOF_QUALITY,
  132.                                                DEFAULT_PITCH | FF_DONTCARE,
  133.                                                "Times New Roman" );
  134.  
  135.  
  136.                            // Retrieve the device context and
  137.                            // select the font into it.
  138.                            //................................
  139.                            hDC    = GetDC( hWnd );
  140.                            hOldFont = SelectObject( hDC, hFont );
  141.                           
  142.                            // Retrieve the number of kerning pairs
  143.                            // and allocate memory to hold them.
  144.                            //..................................
  145.                            dwRet = GetKerningPairs( hDC, 0, NULL );
  146.                            lpKp = GlobalAlloc( GPTR, dwRet * 
  147.                                                sizeof( KERNINGPAIR ) );
  148.  
  149.                            // Retrieve the actual kerning pairs.
  150.                            //...................................
  151.                            GetKerningPairs( hDC, dwRet, lpKp );
  152.                      
  153.                            nTabs[0] = 50;
  154.                            nTabs[1] = 100;
  155.  
  156.                            // Display all the kerning pairs.
  157.                            //...............................
  158.                            for( i = 0; i < (int)dwRet; i++ )
  159.                            {
  160.                                wsprintf( szMsg, 
  161.                                          "First: %c\tSecond: %c\tKerning: %d",
  162.                                          (lpKp+i)->wFirst,
  163.                                          (lpKp+i)->wSecond,
  164.                                          (lpKp+i)->iKernAmount );
  165.  
  166.                               TabbedTextOut( hDC, 10, i*16, szMsg, 
  167.                                              strlen( szMsg ),
  168.                                              2, nTabs, 10 );
  169.                            }             
  170.                            
  171.                            GlobalFree( lpKp );
  172.                            SelectObject( hDC, hOldFont );
  173.                            ReleaseDC( hWnd, hDC );
  174.                            DeleteObject( hFont );
  175.                         }
  176.                         break;
  177.  
  178.                  case IDM_ABOUT :
  179.                         DialogBox( hInst, "AboutBox", hWnd, (DLGPROC)About );
  180.                         break;
  181.  
  182.                  case IDM_EXIT :
  183.                         DestroyWindow( hWnd );
  184.                         break;
  185.               }
  186.               break;
  187.       
  188.       case WM_DESTROY :
  189.               PostQuitMessage(0);
  190.               break;
  191.  
  192.       default :
  193.             return( DefWindowProc( hWnd, uMsg, wParam, lParam ) );
  194.    }
  195.  
  196.    return( 0L );
  197. }
  198.  
  199.  
  200. LRESULT CALLBACK About( HWND hDlg,           
  201.                         UINT message,        
  202.                         WPARAM wParam,       
  203.                         LPARAM lParam)
  204. {
  205.    switch (message) 
  206.    {
  207.        case WM_INITDIALOG: 
  208.                return (TRUE);
  209.  
  210.        case WM_COMMAND:                              
  211.                if (   LOWORD(wParam) == IDOK         
  212.                    || LOWORD(wParam) == IDCANCEL)    
  213.                {
  214.                        EndDialog(hDlg, TRUE);        
  215.                        return (TRUE);
  216.                }
  217.                break;
  218.    }
  219.  
  220.    return (FALSE); 
  221. }
  222.